<?php
namespace Buu\Modules\HaohaoceManager\Lib;
use Doraemon\tools\Log;

class K8sModel
{
	const STATUS_PENDING = 'Pending';
	const STATUS_CONTAINERCREATING = 'ContainerCreating';
	const STATUS_CRASHLOOPBACKOFF = 'CrashLoopBackOff';
	const STATUS_RUNNING = 'Running';
	const K8S_NAMESPACE = 'hhc';
	
	static public function getPods(){
		$cmd = 'kubectl get pods -o wide -n '.self::K8S_NAMESPACE;
		$rs = self::_run_cmd($cmd);
		$lines = explode("\n" , $rs);
		unset($lines[0]);
		$rs = array();
		$pods = array();
		foreach ($lines as $line) {
			$line = trim($line);
			if($line){
				
				$rs = self::_format_pod_info($line);
				
				$pods[$rs['pod_name']] = $rs;
			}
		}
		return $pods;
	}
	static public function getSvcs(){
		$cmd = 'kubectl get svc -o wide -n '.self::K8S_NAMESPACE;
		$rs = self::_run_cmd($cmd);

		$lines = explode("\n" , $rs);
		unset($lines[0]);
		
		$rs = array();
		$pods = array();
		foreach ($lines as $line) {
			$line = trim($line);
			if($line){
				$rs = self::_format_svc_info($line);
				if($rs['svc_name'] == 'kubernetes'){
					continue;
				}
				$pods[$rs['svc_name']] = $rs;
			}
		}
		return $pods;
	}
	static public function getPodinfoByPodid($pod_name){
		$cmd = 'kubectl get pods -o wide  -n '.self::K8S_NAMESPACE.' | grep '.$pod_name;
		$rs = self::_run_cmd($cmd);
		$rs = self::_format_pod_info($rs);
		return $rs;
	}
	static public function getSvcinfoByid($svc_name){
		$cmd = 'kubectl get svc -o wide  -n '.self::K8S_NAMESPACE.' | grep '.$svc_name;
		$rs = self::_run_cmd($cmd);
		$rs = self::_format_svc_info($rs);
		return $rs;
	}
	static public function descPod($pod_name){
		$cmd = 'kubectl describe pod '.$pod_name. ' -n '.self::K8S_NAMESPACE;
		return self::_run_cmd($cmd);
	}
	static public function descSvc($svc_name){
		$cmd = 'kubectl describe svc '.$svc_name.' -n '.self::K8S_NAMESPACE;
		return self::_run_cmd($cmd);
	}
	static public function createPod($yaml_path , &$pod_name){
		$cmd = 'kubectl create -f '.$yaml_path;
		
		$rs = trim(self::_run_cmd($cmd));
		if(substr($rs,-7) == 'created'){
			preg_match('/\/([0-9a-z\-]+) /' , $rs , $b);
			$pod_name = $b[1];
			$a = self::getPodinfoByPodid($pod_name);
			
			$pod_name = $a['pod_name'];
			return true;
		}else{
			return $rs;
		}	
	}
	static public function createSvc($yaml_path , &$svc_name){
		$cmd = 'kubectl create -f '.$yaml_path;
		$rs = trim(self::_run_cmd($cmd));
		if(substr($rs,-7) == 'created'){
			preg_match('/\/([0-9a-z\-]+) /' , $rs , $b);
			$svc_name = $b[1];
			return true;
		}else{
			return $rs;
		}
	}
	static public function deletePod($yaml_path){
		$cmd = 'kubectl delete -f '.$yaml_path.' --wait=false';
		$time = time();
		K8sView::running_log('delete pod cmd: '.$cmd);
		$rs = self::_run_cmd($cmd);
		K8sView::running_log('time:'.(time()-$time));
		K8sView::running_log('delete pod result: '.$rs);
		if(substr($rs , -7) == 'deleted'){
			return true;
		}else{
			return $rs;
		}
	}
	static public function deleteSvc($yaml_path){
		$cmd = 'kubectl delete -f '.$yaml_path.' --wait=false';
		$time = time();
		K8sView::running_log('delete svc cmd: '.$cmd);
		$rs = self::_run_cmd($cmd);
		K8sView::running_log('time:'.(time()-$time));
		K8sView::running_log('delete pod result: '.$rs);
		if(substr($rs , -7) == 'deleted'){
			return true;
		}else{
			return $rs;
		}
	}

	static public function execByPodname($podName , $container_name, $cmd){
		$cmd = 'kubectl exec -it '.$podName.' --container='.$container_name.' -n '.self::K8S_NAMESPACE.' -- /bin/sh -c "'.$cmd.'"';
		// K8sView::running_log($cmd);
		$rs = self::_run_cmd($cmd);
		// K8sView::running_log($rs);
		return $rs;
	}

	static private function _run_cmd($cmd){
		$fp = popen($cmd , 'r');
		while (!feof($fp)){
			$rs .= fread($fp , 4096);
		};
		Log::info('k8s_model:'.$cmd .' '.$rs);
		return $rs;
	}
	static private function _format_pod_info($line){
		$line = preg_replace('/[ ]+/', "\t", $line);
		list($rs['pod_name'] 
					, $rs['pod_ready'] 
					, $rs['pod_status'] 
					, $rs['pod_restart'] 
					, $rs['pod_age'] 
					, $rs['pod_ip'] 
					, $rs['pod_node']) = explode("\t", $line);
		return $rs;
	}
	static private function _format_svc_info($line){
		$line = preg_replace('/[ ]+/', "\t", $line);
		list($rs['svc_name'] 
					, $rs['svc_type'] 
					, $rs['svc_cluster_ip'] 
					, $rs['svc_external_ip'] 
					, $rs['svc_port'] 
					, $rs['svc_age'] 
					, $rs['svc_selector']) = explode("\t", $line);
		return $rs;
	}
}